/*
 * BlackPanel.java
 *
 */

package drayson.weboca.gui;

import drayson.weboca.Utils;
import java.awt.Component;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JFileChooser;
import org.netbeans.spi.wizard.WizardPage;

/**
 * A GUI panel used to allow the user to specify words that should not be included in the tuple searches
 *
 * @author Michael Drayson
 * @version 1.0
 */
public class BlackPanel extends WizardPage {
    
    private JFileChooser inChooser;
    private JFileChooser outChooser;
    
    /**
     * Creates new form BlackPanel using the Wizard framework and initialises the objects on it
     * Also sets up some default variables to be stored within the wizard frameworks data storage facility
     */
    public BlackPanel() 
    {
        super("Seeds", "Seeds");
        initComponents();
        
        // Set up some defaults variables
        putWizardData("blackWords", " ");
    }
    
    /**
     * Returns the description of the BlackPanel GUI form
     *
     * @return The description of the BlackPanel form
     */
    public static String getDescription() 
    {
        return "Seeds";
    }
    
    /**
     * Validates the form to ensure that if the advanced checkbox is selected,  a black list must have been saved
     *
     * @param component The component being validated
     * @param o The object being validated
     * @return The error presented by the Wizard framework if the condition isn't met
     */
    protected String validateContents (Component component, Object o) 
    {   
        if (advancedBox.isSelected())
        {
            if (getWizardData("blacksaved") == "false")
            {
                return "You must save the black list before continuing... ";
            }
        }
        return null;
    }
    
   /**
    * Returns a file chooser to be used to load a string from a file
    *
    * @return A JFileChooser for opening files
    */
    private JFileChooser getInChooser() 
    {
        if (inChooser == null) 
        {
            inChooser = new JFileChooser();
        }
        return inChooser;
    }
    
    
   /**
    * Returns a file chooser to be used to save a string to a file
    *
    * @return A JFileChooser for saving files
    */
    private JFileChooser getOutChooser() 
    {
        if (outChooser == null) 
        {
            outChooser = new JFileChooser();
        }
        return outChooser;
    }
    
    
    /**
     * Method that enables or disables a range of GUI elements, and puts  
     * data into the wizard.
     * <p>
     * Method can be used both ways - to enable and disable features.
     */
    private void advancedClicked()
    {
         if (advancedBox.isSelected() == false)
        {
            putWizardData("UsingBlackList", "false");
            saveButton.setEnabled(false);
            whiteLabel.setEnabled(false);
            blackWords.setEnabled(false);
            blackWords.setOpaque(false);
            btnLoad.setEnabled(false);
            btnSave.setEnabled(false);  
        }
        if (advancedBox.isSelected() == true)
        {
            putWizardData("UsingBlackList", "true");
            
            // Reset the state "saved" in the wizard to false
            putWizardData("blacksaved", "false");
            saveButton.setEnabled(true);
            whiteLabel.setEnabled(true);
            blackWords.setEnabled(true);
            blackWords.setOpaque(true);
            btnLoad.setEnabled(true);
            btnSave.setEnabled(true);    
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        btnLoad = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        whiteLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        blackWords = new javax.swing.JTextArea();
        jSeparator1 = new javax.swing.JSeparator();
        advancedBox = new javax.swing.JCheckBox();
        saveButton = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JSeparator();

        btnLoad.setText("Load custom");
        btnLoad.setEnabled(false);
        btnLoad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoadActionPerformed(evt);
            }
        });

        btnSave.setText("Save custom");
        btnSave.setEnabled(false);
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        whiteLabel.setText("Enter all black listed words, one word per line");
        whiteLabel.setEnabled(false);

        blackWords.setColumns(20);
        blackWords.setRows(20);
        blackWords.setEnabled(false);
        blackWords.setName("blackWords");
        blackWords.setOpaque(false);
        //txtSeeds.setText("arabic\nconcordance\nlanguage\nlearning\ncluster");
        blackWords.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                blackWordsFocusLost(evt);
            }
        });

        jScrollPane1.setViewportView(blackWords);

        advancedBox.setText("Enable Black List");
        advancedBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        advancedBox.setMargin(new java.awt.Insets(0, 0, 0, 0));
        advancedBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                advancedBoxactionPerformed(evt);
            }
        });

        saveButton.setText("Save List!");
        saveButton.setEnabled(false);
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 275, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 16, Short.MAX_VALUE)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(btnSave)
                            .add(btnLoad)))
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jSeparator2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(layout.createSequentialGroup()
                                .addContainerGap()
                                .add(advancedBox)))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 210, Short.MAX_VALUE)
                        .add(saveButton))
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(whiteLabel)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(saveButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 19, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(advancedBox))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(whiteLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .add(btnLoad)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(btnSave)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED))
                            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE)))
                    .add(jSeparator2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    /**
     * This method saves a variable to the wizard, ensuring that the validation 
     * condition isn't met if something has been changed.
     */
    private void blackWordsFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_blackWordsFocusLost
           putWizardData("blacksaved", "false");
    }//GEN-LAST:event_blackWordsFocusLost
    
    /**
     * Save black list words to the wizard data holder.
     * <p>
     * Method also sets the state to saved in the wizard.
     *
     * @param event The event occuring on the saveButton
     */
    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        // Set the state "saved" in the wizard to true as it's now been saved
        putWizardData("blacksaved", "true");
        
        if (blackWords.getText().length() == 0)
        {
            putWizardData("blackWords", " ");
        }
        else
        {
            putWizardData("blackWords", blackWords.getText());
        }    
    }//GEN-LAST:event_saveButtonActionPerformed

 
   /**
     * This method toggles the advancedClick method when the advanced box is checked/unchecked 
     *
     * @param event The event occuring on the advancedBox
     */
    private void advancedBoxactionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_advancedBoxactionPerformed
      advancedClicked();
    }//GEN-LAST:event_advancedBoxactionPerformed

    /**
     * This method attempts to open a JFileChooser in order to save the blacklist to a file
     *
     * @param event The event occuring on the saveButton
     * @exception e If the select file could not be found or I/O error
     */
    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed

        int returnVal = getOutChooser().showOpenDialog(this);
    
        if(returnVal == JFileChooser.APPROVE_OPTION) {
            try {
                Utils.saveFile(blackWords.getText(), getOutChooser().getSelectedFile());
                
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        
        putWizardData("blacksaved", "false");
    }//GEN-LAST:event_btnSaveActionPerformed

    /**
     * This method attempts to open a JFileChooser in order to load a blacklist from a file
     *
     * @param event The event occuring on the loadButton
     * @exception e If the select file could not be found or I/O error
     */
    private void btnLoadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoadActionPerformed

        int returnVal = getInChooser().showOpenDialog(this);
    
        if(returnVal == JFileChooser.APPROVE_OPTION) {
            try {
                blackWords.setText(Utils.loadFile(getInChooser().getSelectedFile()).trim());
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        
                   putWizardData("blacksaved", "false");
    }//GEN-LAST:event_btnLoadActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox advancedBox;
    protected javax.swing.JTextArea blackWords;
    private javax.swing.JButton btnLoad;
    private javax.swing.JButton btnSave;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JButton saveButton;
    private javax.swing.JLabel whiteLabel;
    // End of variables declaration//GEN-END:variables
    
}
